<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
    <#assign r = r"#{" />
    <#assign l = r"}" />
    <!-- 通用查询映射结果 -->
    <resultMap id="baseResultMap" type="${package.Entity}.${entity}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
                <id column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
            <result column="${field.name}" property="${field.propertyName}" />
        </#list>
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
                <result column="${field.name}" property="${field.propertyName}" />
            </#if>
        </#list>
    </resultMap>

    <!-- 返回总记录数 start -->
    <resultMap id="countResultMap" type="java.lang.Integer">
        <result column="totalCount" jdbcType="INTEGER" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="baseColumnList">
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
        ${table.fieldNames}
    </sql>


    <!-- 分页查询条件 -->
    <select id="queryByPage" resultMap="baseResultMap">
        SELECT
        <include refid="baseColumnList"/>
        FROM ${table.name}
        <where>
            <#list table.fields as field>
                <if test="${entity?uncap_first}.${field.propertyName} != null and ${entity?uncap_first}.${field.propertyName} != ''">
                    AND ${field.columnName} LIKE CONCAT('%', ${r}${entity?uncap_first}.${field.propertyName}${l}, '%')
                </if>
            </#list>
        </where>
        ORDER BY create_time DESC
        limit
        ${r}pageStart${l},${r}pageSize${l}
    </select>

    <!-- 查询总记录数 -->
    <select id="queryByCount" resultMap="countResultMap">
        SELECT
        COUNT(1) as totalCount
        FROM ${table.name}
        <where>
            <#list table.fields as field>
                <if test="${field.propertyName} != null and ${field.propertyName} != ''">
                    AND ${field.columnName} LIKE CONCAT('%', ${r}${field.propertyName}${l}, '%')
                </if>
            </#list>
        </where>
    </select>


</mapper>
